import mx.events.StateChangeEvent;
import com.adobe.cairngorm.control.CairngormEventDispatcher;
import net.optix.ip.events.EditFirmEvent;
import net.optix.ip.vo.FirmVO;

import net.optix.ip.model.ModelLocator;
        	
import mx.events.CloseEvent; 
import mx.managers.PopUpManager;
import mx.events.ValidationResultEvent;
            
[Bindable]
public var model:ModelLocator	=	ModelLocator.getInstance(); 
  
private function titleWindow_close(event:CloseEvent):void 
{ 
	model.result	=	"";
	model.bSubmit	=	true;
 	PopUpManager.removePopUp(this); 
}
            
public function init():void
{
            	model.close	=	"no";
}
            
public function editFirm():void
{	
	var result:Boolean	=	true;

	var resultValidate:ValidationResultEvent;
				
	resultName.text	=	"";
	resultAddress.text		=	"";
	resultCity.text	=	"";
	resultState.text	=	"";
	resultCountry.text		=	"";
	resultPostalCode.text	=	"";
				
	model.result			=	"";
				
	resultValidate	=	stringValidator.validate(firmName.text);
	if(resultValidate.type == ValidationResultEvent.INVALID)
  	{
    	result	=	false;
    	resultName.text	=	
            			resultValidate.message;
	}
            	
  	resultValidate	=	stringValidator.validate(address.text);
  	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultAddress.text	=	
            			resultValidate.message;
            	}
            	
	resultValidate	=	stringValidator.validate(city.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultCity.text	=	
            			resultValidate.message;
            	}
            	
	resultValidate	=	stringValidator.validate(state.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultState.text	=	
            			resultValidate.message;
            	}
            	
	resultValidate	=	stringValidator.validate(country.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultCountry.text	=	
            			resultValidate.message;
            	}
            	
	resultValidate	=	postalValidator.validate(postalCode.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultPostalCode.text	=	
            			resultValidate.message;
            	}
            	
	model.bSubmit	=	false;
	model.result	=	"";
            	
            	
            	
	if(result == true)
	{
    	var vo:FirmVO	=	model.firm;
    	vo.name			=	firmName.text;
    	vo.address		=	address.text;
       	vo.city			=	city.text;
      	vo.state		=	state.text;
     	vo.country		=	country.text;
     	vo.postalCode	=	postalCode.text;
            		
    	var event:EditFirmEvent	=	new EditFirmEvent(vo);
     	CairngormEventDispatcher.getInstance().dispatchEvent(event);
    	}
  	else
   	{
     	model.bSubmit	=	true;
  	}
}